const rp = require('request-promise')
const getAccessToken = require('./getAccessToken.js')

const callCloudFn = async (ctx,fnName,params)=>{
    const ACCESS_TOKEN = await  getAccessToken()  
    const options ={
        method:'POST',
        uri:`https://api.weixin.qq.com/tcb/invokecloudfunction?access_token=${ACCESS_TOKEN}&env=${ctx.state.env}&name=${fnName}`,
        body:{
           ...params
        },
        json:true
    }
    return await  rp(options)
    .then(res=>{
        return res
    })
    .catch(err=>{
        console.log(err,'请求fail的返回值');
    })
  
}

module.exports = callCloudFn